/*
 *    Copyright (c) 2018-2025, scs All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: scs
 */
package com.zyh.scs.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyh.scs.admin.api.entity.SysPost;
import com.zyh.scs.admin.api.vo.PostExcelVO;
import com.zyh.scs.admin.api.vo.PostNewExcelVO;
import com.zyh.scs.admin.mapper.SysPostMapper;
import com.zyh.scs.admin.service.SysPostService;
import com.zyh.scs.common.core.exception.OpenException;
import com.zyh.scs.common.core.exception.OpenResponseCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 岗位信息表
 *
 * @author fxz
 * @date 2022-03-26 12:50:43
 */
@Service
@RequiredArgsConstructor
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    private final SysPostMapper sysPostMapper;

    /**
     * 导入岗位
     *
     * @param excelVOList   岗位列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @Override
    public Boolean importPost(List<PostNewExcelVO> excelVOList, BindingResult bindingResult) {

        // 执行数据插入操作 组装 PostDto
        for (PostNewExcelVO excel : excelVOList) {
            if (excel == null) {
                continue;
            }
            if (StrUtil.isBlank(excel.getPostName())) {
                throw new OpenException(OpenResponseCode.EXCEPTION.getCode(),"职务名称不能为空");
            }
            SysPost sysPost = new SysPost();
            BeanUtil.copyProperties(excel, sysPost);
            sysPostMapper.insert(sysPost);
        }
        return Boolean.TRUE;
    }

    /**
     * 导出excel 表格
     *
     * @return
     */
    @Override
    public List<PostNewExcelVO> listPost(SysPost query, Long[] ids) {
        List<SysPost> postList = this
                .list(Wrappers.lambdaQuery(query).in(ArrayUtil.isNotEmpty(ids), SysPost::getPostId, ids));
        // 转换成Excel 对象输出
        AtomicInteger integer = new AtomicInteger(1);
        return postList.stream().map(post -> {
            PostNewExcelVO postExcelVO = new PostNewExcelVO();
            BeanUtil.copyProperties(post, postExcelVO);
            postExcelVO.setIndex(integer.getAndIncrement());
            return postExcelVO;
        }).collect(Collectors.toList());
    }

    /**
     * 插入excel Post
     */
    private void insertExcelPost(PostExcelVO excel) {
        SysPost sysPost = new SysPost();
        BeanUtil.copyProperties(excel, sysPost);
        this.save(sysPost);
    }

}
